﻿/*
   Copyright 2010 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*
 * EngineeringMaterialsChecksum
 * 
 * Helper methods for computing SHA1 and MD5 sums on files or streams
 * 
 * Dr. Richard Boardman
 * School of Engineering Sciences
 * Copyright (c) 2010 University of Southampton
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace uk.ac.soton.ses
{
    /// <summary>
    /// Helper methods for computing SHA1 and MD5 sums on files or streams
    /// </summary>
    public class Checksum
    {
        /// <summary>
        /// Computes the SHA1 sum for the file <code>filename</code>
        /// </summary>
        /// <param name="filename">Name of the file to SHA1 sum</param>
        /// <returns>Hexadecimal string representation of the checksum in lowercase, no separators</returns>
        public static string GetSHA1Sum(string filename)
        {
            string sha1;
            using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                 sha1 = GetSHA1Sum(fs);
            }

            return sha1;
        }

        /// <summary>
        /// Computes the SHA1 sum for the stream
        /// </summary>
        /// <param name="stream">Stream to SHA1 sum</param>
        /// <returns>Hexadecimal string representation of the checksum in lowercase, no separators</returns>
        public static string GetSHA1Sum(Stream stream)
        {
            SHA1 sha1 = SHA1.Create();
            sha1.ComputeHash(stream);
            return BitConverter.ToString(sha1.Hash).Replace("-", "").ToLower();
        }

        /// <summary>
        /// Computes the SHA1 checksum for the file, and compares that to the supplied checksum
        /// </summary>
        /// <param name="sha1sum">The checksum</param>
        /// <param name="filename">The file to check</param>
        /// <returns><value>true</value> if they match, else <value>false</value></returns>
        public static Boolean CompareSHA1(string sha1sum, string filename)
        {
            return sha1sum.Equals(GetSHA1Sum(filename));
        }

        /// <summary>
        /// Computes the SHA1 sum for the stream, and compares that to the supplied checksum
        /// </summary>
        /// <param name="sha1sum">The checksum</param>
        /// <param name="stream">The stream to check</param>
        /// <returns><value>true</value> if they match, else <value>false</value></returns>
        public static Boolean CompareSHA1(string sha1sum, Stream stream)
        {
            return sha1sum.Equals(GetSHA1Sum(stream));
        }

        /// <summary>
        /// Computes the MD5 sum for the file <code>filename</code>
        /// </summary>
        /// <param name="filename">Name of the file to MD5 sum</param>
        /// <returns>Hexadecimal string representation of the checksum in lowercase, no separators</returns>
        public static string GetMD5Sum(string filename)
        {
            string md5;
            using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                md5 = GetMD5Sum(fs);
            }
            return md5;
        }

        /// <summary>
        /// Computes the MD5 sum for the stream
        /// </summary>
        /// <param name="stream">Stream to MD5 sum</param>
        /// <returns>Hexadecimal string representation of the checksum in lowercase, no separators</returns>
        public static string GetMD5Sum(Stream stream)
        {
            MD5 md5 = MD5.Create();
            md5.ComputeHash(stream);
            return BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
        }

        /// <summary>
        /// Computes the MD5 checksum for the file, and compares that to the supplied checksum
        /// </summary>
        /// <param name="sha1sum">The checksum</param>
        /// <param name="filename">The file to check</param>
        /// <returns><value>true</value> if they match, else <value>false</value></returns>
        public static Boolean CompareMD5(string sha1sum, string filename)
        {
            return sha1sum.Equals(GetMD5Sum(filename));
        }

        /// <summary>
        /// Computes the MD5 sum for the stream, and compares that to the supplied checksum
        /// </summary>
        /// <param name="sha1sum">The checksum</param>
        /// <param name="stream">The stream to check</param>
        /// <returns><value>true</value> if they match, else <value>false</value></returns>
        public static Boolean CompareMD5(string sha1sum, Stream stream)
        {
            return sha1sum.Equals(GetMD5Sum(stream));
        }
    }
}
